package com.sxj.web.jsp;

import com.sxj.web.entity.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 石小俊
 * @date 2024年03月09日 15:16
 */
@WebServlet("*.session")
public class SessionServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        if("/add.session".equals(path)){
            addSession(request,response);
        }
        if("/delete.session".equals(path)){
            deleteSession(request,response);
        }
        if("/clear.session".equals(path)){
            clearSession(request,response);
        }
        if("/modify.session".equals(path)){
            modifySession(request,response);
        }
        if("/find.session".equals(path)){
            findSession(request,response);
        }
    }

    public void addSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        // void setAttribute(String var1, Object var2)
        // 保存的数据类型是Object,可以是任意类型
        Emp emp = new Emp();
        emp.setId(1);
        emp.setName("admin");
        emp.setSalary(8000.0);
        session.setMaxInactiveInterval(5);
        session.setAttribute("emp",emp);
        session.setAttribute("msg","Hello HttpSession");
    }

    public void deleteSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 在cookie中通过生存时间进行删除
        // 在session,默认情况下,session的生存时间是半小时
        // 可以修改,但是实际开发中不建议修改
        HttpSession session = request.getSession();
        // 注意点:在session中将生存时间设置为0并不是删除
        // 设置为0之后表示的是永不过期
//        session.setMaxInactiveInterval(0);
        // 在session中提供了删除的方法
        // 删除指定key的数据
        session.removeAttribute("msg");
    }

    public void clearSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 可以删除所有session数据
        // 该方法其实不是删除所有数据
        // 该方法存在两步:
        // 1.销毁当前session对象
        // 2.创建一个新的session对象
        request.getSession().invalidate();
    }

    public void modifySession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 在session中没有修改的API,但是遇到同名的key时会覆盖原来的
        // 因此,修改的本质也是添加,只是添加的数据的key一致
        Emp emp = new Emp();
        emp.setId(2);
        emp.setName("alice");
        emp.setSalary(9000.0);
        request.getSession().setAttribute("emp",emp);
    }

    public void findSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Emp emp = (Emp) session.getAttribute("emp");
//        System.out.println(emp.getId()+"---"+emp.getName()+"---"+emp.getSalary());
        System.out.println(emp);
        String msg = (String) session.getAttribute("msg");
        System.out.println("msg:"+msg);
    }
}
